#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <conio.h>

//声明全局变量
int up = 1, right = 2, down = 3, left = 4;//上下左右
int high = 20, wide = 50;//界定区域
int x, y;//头部坐标
int fx, fy;//果实坐标
int dir;//方向
int len = 0;//初态蛇身长度
int scoretest = 0;//初始分数

int book[50][50] = {0};
int tailx[200] = {0}, taily[200] = {0};

//gotoxy函数
void gotoxy(int x, int y) {
	COORD coord;
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

//打印水果
void fruit() {
	fx = rand() % (wide - 2) + 1;
	fy = rand() % (high - 2) + 1;
}

//检查游戏进程
int check() {
	if (x < 1 || x >= wide - 1 || y < 1 || y >= high - 1 || book[y][x] == 1) {
		return 1;
	}

	if (x == fx && y == fy) {
		fruit();
		scoretest++;
		len++;
		return 0;
	} else {
		return 0;
	}

	return 0;
}

//打印棋盘
void print() {
	Sleep(120);//运动速度，越大越慢
	gotoxy(0, 0);
	for (int i = 0; i < high; i++) {
		for (int j = 0; j < wide; j++) {
			if (i == 0 || i == high - 1 || j == 0 || j == wide - 1) {
				printf("#");
			} else if (i == y && j == x) {
				printf("O");
			} else if (i == fy && j == fx) {
				printf("*");
			} else if (book[i][j] == 1) {
				printf("o");
			} else
				printf(" ");
		}
		printf("\n");
	}
}

//检测指令
void control() {
	if (_kbhit()) {
		char ch = getch();
		switch (ch) {
			case 'w':
			case 'W':
				if (dir != down)
					dir = up;
				break;
			case 'a':
			case 'A':
				if (dir != right)
					dir = left;
				break;
			case 's':
			case 'S':
				if (dir != up)
					dir = down;
				break;
			case 'd':
			case 'D':
				if (dir != left)
					dir = right;
				break;
		}
	}
}

//控制移动
void move() {
	if (len > 0) {
		for (int i = len; i > 0; i--) {
			tailx[i] = tailx[i - 1];
			taily[i] = taily[i - 1];
		}
		tailx[0] = x; //新坐标x y交给tail第0个元素
		taily[0]  = y;
		book[taily[0] ][tailx[0] ] = 1;
		book[taily[len] ][tailx[len] ] = 0; //末尾坐标取消标记
	}

	switch (dir) {
		case 1:
			y--;
			break;
		case 2:
			x++;
			break;
		case 3:
			y++;
			break;
		case 4:
			x--;
			break;
	}
}


int main() {
	
	return 0;
}